package com.ql.mangersystem.controller;

import cn.hutool.core.date.DateUtil;
import com.ql.mangersystem.common.HoneyLogs;
import com.ql.mangersystem.common.LogType;
import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.dto.NoticeAddDTO;
import com.ql.mangersystem.entity.dto.NoticePgeDTO;
import com.ql.mangersystem.entity.dto.NoticeUpdateDTO;
import com.ql.mangersystem.entity.dto.PageDTO;
import com.ql.mangersystem.entity.po.Notice;
import com.ql.mangersystem.entity.po.User;
import com.ql.mangersystem.service.NoticeService;
import com.ql.mangersystem.service.UserService;
import com.ql.mangersystem.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/notice")
@RestController
@Slf4j
@Api("公告接口")
@CrossOrigin(value = "http://localhost:5173/")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private UserService userService;

    @ApiOperation("新增信息")
    @PostMapping("/add")
    @HoneyLogs(operation = "公告",type = LogType.ADD)
    public Result add(@RequestBody NoticeAddDTO noticeAddDTO) {
        //获取当前登录用户
        User currentUser = TokenUtils.getCurrentUser();
//        System.out.println(currentUser);
        assert currentUser != null;
        noticeAddDTO.setUserid(currentUser.getId());
        noticeAddDTO.setTime(DateUtil.now());
//        System.out.println(noticeAddDTO);
        int result = noticeService.save(noticeAddDTO);
        if (result <= 0) {
            return Result.error("新增失败");
        }
        return Result.success();
    }
    @ApiOperation("修改信息")
    @PutMapping("/update")
    @HoneyLogs(operation = "公告",type = LogType.UPDATE)
    public Result update(@RequestBody NoticeUpdateDTO noticeUpdateDTO) {
//        System.out.println(noticeUpdateDTO);
        int result= noticeService.update(noticeUpdateDTO);
        if (result <= 0) {
            return Result.error("修改失败");
        }
        return Result.success();
    }
    @ApiOperation("删除信息")
    @DeleteMapping("/delete/{id}")
    @HoneyLogs(operation = "公告",type = LogType.DELETE)
    public Result delete(@PathVariable Integer id) {
        int result = noticeService.delete(id);
        if (result <= 0) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
    @ApiOperation("批量删除")
    @DeleteMapping("/delete/batch")
    @HoneyLogs(operation = "公告",type = LogType.BATCH_DELETE)
    public Result deleteBatch(@RequestParam("ids") List<Integer> ids) {
//        System.out.println(ids);
        int result = noticeService.deleteBatch(ids);
        if (result <= 0) {
            return Result.error("批量删除失败");
        }
        return Result.success();
    }
    @ApiOperation("查询全部信息")
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Notice> list = noticeService.selectAll();
        if (list == null || list.isEmpty()) {
            return Result.success();
        }
        return Result.success(list);
    }
    @ApiOperation("根据id查询信息")
    @GetMapping("/select/{id}")
    public Result select(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        User user = userService.getUserById(notice.getUserid());
//        System.out.println(user);
        if (user != null) {
            notice.setUsername(user.getUsername());
        }
//        System.out.println(notice);
        return Result.success(notice);
    }
    @ApiOperation("分页模糊查询")
    @PostMapping("/selectAll")
    public Result selectAll(@RequestBody NoticePgeDTO noticePgeDTO) {
        Page<Notice> page = noticeService.page(noticePgeDTO);
        return Result.success(page);
    }
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public Result page(@RequestBody PageDTO pageDTO) {
        Page<Notice> page = noticeService.pageAll(pageDTO);
        return Result.success(page);
    }
    @ApiOperation("修改open")
    @PutMapping("/changeOpen")
    @HoneyLogs(operation = "公告",type = LogType.UPDATE)
    public Result changeOpen(@RequestParam("open") Integer open,@RequestParam("id") Integer id) {
//        System.out.println(open);
//        System.out.println(id);
        int result = noticeService.changeOpen(open,id);
        if (result <= 0) {
            return Result.error("修改失败");
        }
//        System.out.println(result);
        return Result.success();
    }
}
